<?php

include "header.inc.php";

$rememberInfo = "&remeber_username=$reg_username&remember_display_name=$reg_display_name&remember_name=$reg_full_name&remember_email=$reg_email&remember_location=$reg_location&remember_month=$reg_month&remember_day=$reg_day&remember_year=$reg_year&remember_sig=$reg_sig&remeber_profile=$reg_profile";

if ((!$reg_username) OR (!$reg_pass1) OR (!$reg_pass2) OR (!$reg_email))
{
	die(header(error("register.php?game=$game$rememberInfo","Please fill in all required information.")));
}

if ($HTTP_COOKIE_VARS[referer]) { $REFER_PERSON = "$HTTP_COOKIE_VARS[referer]"; }

$reg_username = strtolower($reg_username);

if ($referer) { $REFER_PERSON = "$referer"; }
if ($REFER_PERSON == "$reg_username") { $REFER_PERSON = ""; }

$check_username = fetch("SELECT id FROM members2 WHERE username = '$reg_username' AND game = '$game'");

if ($check_username[id])
{
	die(header(error("register.php?game=$game$rememberInfo","The username you have selected alredy exists. Please try again.")));
}
if ($reg_pass1 != $reg_pass2)
{
	die(header(error("register.php?game=$game$rememberInfo","Your passwords did not match. Passwords ARE case sensitive.")));
}
if (!$reg_display_name) { $reg_display_name = $reg_username; }
$newDisplay = strtolower(ereg_replace(" ","","$reg_display_name"));
if ($newDisplay != $reg_username)
{
	die(header(error("register.php?game=$game$rememberInfo","Your display name must contain the exact same letters as your username with the exception of spaces and capitals.")));
}
$md5pass = md5($reg_pass1);
if (
($this_year-$reg_year >= 13) OR (($this_year-$reg_year == 12) AND
($this_month-$reg_month >= 1)) OR (($this_year-$reg_year == 12) AND
($this_month-$reg_month == 0) AND ($today-$reg_day >= 0)))
{
	$rank = 3;
}
else
{
	$rank = 2;
}

if (($reg_month < 1) OR ($reg_month > 12) OR ($reg_day < 1) OR ($reg_day > 31) OR ($reg_year < 0) OR ($reg_year > $this_year))
{
	die(header(error("register.php?game=$game$rememberInfo","Please choose a VALID bithdate.")));
}

$reg_username = badwords($reg_username);

if (($reg_gender != "1") AND ($reg_gender != "2"))
{
	die(header(error("register.php?game=$game$rememberInfo","Please choose a VALID gender.")));
}

$reg_profile = smilies(badwords(strip_tags($reg_profile,"<b><i><u><a><font><img><p><br><body><background>")));
$reg_sig = badwords(strip_tags($reg_sig,"<b><i><u><a><font>"));

if (preg_match('/^[a-z0-9_]*$/UD',$reg_username))
{
	mysql_query("INSERT INTO members2 (username,password,display_name,rank,referer,game) VALUES ('$reg_username','$md5pass','$reg_display_name','$rank','$REFER_PERSON','$game')");
	$insertMemberID = mysql_insert_id();
	mysql_query("INSERT INTO members_profiles2 (username,email,real_name,birthday,date_registered,location,gender,profile,signature,game) VALUES ('$reg_username','$reg_email','$reg_full_name','$reg_month-$reg_day-$reg_year','$timestamp','$reg_location','$reg_gender','$reg_profile','$reg_sig','$game')");
}
else
{
	die(header(error("register.php?game=$game$rememberInfo","Your username can only contain a-z, 0-9 and underscores. Please try again.")));
}

if ($REFER_PERSON)
{
	$findReferrer = fetch("SELECT referrals,referrals2 FROM members_profiles2 WHERE username 	= '$REFER_PERSON' AND game = '$game'");
	points($game,"$REFER_PERSON", "250");
	points($game,"$reg_username", "1500");

	mysql_query("UPDATE members_profiles2 SET referrals=$findReferrer[referrals]+1 WHERE 	username = '$REFER_PERSON' AND game = '$game'");
	mysql_query("UPDATE members_profiles2 SET referrals2=$findReferrer[referrals2]+1 WHERE 	username = '$REFER_PERSON' AND game = '$game'");
	setcookie("referer","",time());
}
mysql_query("UPDATE game_tables SET newest_member = '$insertMemberID' WHERE id = '$game'");

setcookie("username_$game",$reg_username,time()+2678400);
setcookie("password_$game",$md5pass,time()+2678400);

die(header(error("checklist.php?game=$game$rememberInfo","Thank you for registering. Using this list should help you out!!")));

?>